package com.winning.sx.microframework.dao.sysmanage;

import com.winning.dapi.data.api.IDataAccessService;
import com.winning.dapi.data.entity.DataTable;
import com.winning.sx.microframework.model.exception.CustomException;
import com.winning.sx.microframework.model.manage.LoginSession;
import com.winning.sx.microframework.model.manage.OPLogModel;
import com.winning.sx.microframework.model.manage.RoleManageModel;
import com.winning.sx.microframework.common.util.BaseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by MJ on 2016-10-31.
 */
@Repository
public class LoginDao {
    @Autowired
    private IDataAccessService dataAccessService;

    public DataTable getSystemList(Map<String, Object> map) throws CustomException {
        try {
            String sql = "SELECT a.cSystemID, cSystemName" +
                    "  FROM PM_SYSTEMS a" +
                    "  LEFT JOIN PM_RoleASSystem b ON a.cSystemID=b.cSystemID" +
                    "  LEFT JOIN PM_ROLES c ON b.cRoleID=c.cRoleID" +
                    "  LEFT JOIN PM_UserASRole d ON d.cRoleID=c.cRoleID" +
                    "  LEFT JOIN PM_USERS e ON d.cUserID=e.cUserID" +
                    "  WHERE e.cUserCode=:userCode";
            //select cSystemID,cSystemName from pm_systems where cSystemID in (select cSystemID from pm_roleassystem where cRoleID in (select cRoleID from pm_userasrole where cUserID=(select cUserID from pm_users where cUserCode=:userName or cUserName=:userName)))
            return this.dataAccessService.querySqlUsingParam(sql, map);
        } catch (Exception e) {
            throw new CustomException(-2001, "获取系统列表失败");
        }
    }

    public LoginSession getLoginUser(String userCode) throws CustomException {
        List<LoginSession> userInfos = null;
        LoginSession userInfo = null;
        try {
            //这里superOrg和org目前全部保存所属机构
            String sql = "SELECT u.cUserID, u.cUserCode, u.cUserName,  u.cPassword,s.id staffId,s.organizationId orgId,s.organizationId superOrgId" +
                    "  FROM PM_USERS u " +
                    "  left join PM_StaffAsUser su on su.userId=u.cUserID " +
                    "  left join BASE_Staff s on s.id=su.staffId " +
                    "  WHERE cUserCode=?";
            userInfos = this.dataAccessService.querySqlForBean(sql, LoginSession.class, userCode);
            userInfo = userInfos.get(0);
        } catch (Exception e) {
            throw new CustomException(-2001, "获取系统列表失败");
        }
        return userInfo;
    }

    public List<RoleManageModel> getLoginRole(String cUserID) throws CustomException {
        List<RoleManageModel> LoginRoles = new ArrayList<RoleManageModel>();
        try {
            String sql = "SELECT a.cRoleID, a.cRoleName, a.cInputTime, a.bState" +
                    "  FROM PM_ROLES a" +
                    "  LEFT JOIN PM_UserASRole b ON a.cRoleID=b.cRoleID" +
                    "  WHERE b.cUserID=?";
            LoginRoles = this.dataAccessService.querySqlForBean(sql, RoleManageModel.class, cUserID);
        } catch (Exception e) {
            throw new CustomException(-2001, "获取系统列表失败");
        }
        return LoginRoles;
    }

    public int setOPLog(OPLogModel opLog) throws CustomException {
        int result = -1;
        try {
            Map opLogMap = BaseUtil.beanToMap(opLog);
            String sql = "INSERT INTO PM_OPLog (cIPAddr,cMACAddr,cOpType,cUserID,cUserCode,cUserName,cOpSys,dOpTime,bSuccess)" +
                    " VALUES(:cIPAddr,:cMACAddr,:cOpType,:cUserID,:cUserCode,:cUserName,:cOpSys,:dOpTime,:bSuccess)";
            result = dataAccessService.executeSqlUsingParam(sql, opLogMap);
        } catch (Exception e) {
            throw new CustomException(-2002, "新增数据失败", e.getMessage(), e.getCause());
        }
        return result;
    }
}
